package com.briup.web.controller;

import com.briup.response.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 *
 * @author by ca
 * @date 2024-11-06  9:56
 */

@RestController
public class CookieController {

    @PostMapping("login2")
    public Result<Cookie> cookie(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        Cookie cookie1 = new Cookie("name",name);
        Cookie cookie2 = new Cookie("password",password);
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        // cookie具有有效期，默认为-1， -1为关闭浏览器自动销毁
        return Result.success();
    }

    @GetMapping("getCookie")
    public Result<String> getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println(name + " " +value);
        }
        return Result.success();
    }
}
